package com.nus.fellowmark.modules.module.service.impl;

import com.nus.fellowmark.dao.ModuleDao;
import com.nus.fellowmark.entity.AssignmentEntity;
import com.nus.fellowmark.entity.ModuleEntity;
import com.nus.fellowmark.modules.assignment.service.AssignmentService;
import com.nus.fellowmark.modules.module.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nus.fellowmark.common.utils.PageUtils;
import com.nus.fellowmark.common.utils.Query;


@Service("moduleService")
public class ModuleServiceImpl extends ServiceImpl<ModuleDao, ModuleEntity> implements ModuleService {

    @Autowired
    ModuleDao moduleDao;

    @Autowired
    AssignmentService assignmentService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ModuleEntity> page = this.page(
                new Query<ModuleEntity>().getPage(params),
                new QueryWrapper<ModuleEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public ModuleEntity getByAssignmentId(Integer assignmentId) {
        AssignmentEntity assignmentEntity = assignmentService.getById(assignmentId);
        Integer moduleId = assignmentEntity.getModuleId();
        return moduleDao.selectById(moduleId);
    }

    @Override
    public ModuleEntity getByCode(String moduleCode) {
        return moduleDao.selectByCode(moduleCode);
    }

}
